package com.woupla.city;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.New;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.solder.logging.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import com.woupla.model.City;
import com.woupla.model.Region;

/**
 * @author Dimitri Lebel
 * 
 */
@Named
@ViewScoped
public class CityMBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5265206894980185627L;

	@Inject
	private CityEJB cityEJB;
	@Inject
	private Logger log;
	@Inject
	@New
	private Region regionSelected;
	@Inject
	@New
	private City citySelected;
	private List<City> cities;
	private String cpSelected;
	private String valueSearch;

	@PostConstruct
	void initialize() {

		cities = new ArrayList<City>();
	}

	/**
	 * @return the citySelected
	 */
	public Region getRegionSelected() {
		return regionSelected;
	}

	/**
	 * @param citySelected
	 *            the citySelected to set
	 */
	public void setRegionSelected(Region regionSelected) {
		this.regionSelected = regionSelected;
	}

	/**
	 * @return the valueSearch
	 */
	public String getValueSearch() {
		return valueSearch;
	}

	/**
	 * @param valueSearch
	 *            the valueSearch to set
	 */
	public void setValueSearch(String valueSearch) {
		this.valueSearch = valueSearch;
	}

	/**
	 * @return the citySelected
	 */
	public City getCitySelected() {
		return citySelected;
	}

	/**
	 * @param citySelected
	 *            the citySelected to set
	 */
	public void setCitySelected(City citySelected) {
		this.citySelected = citySelected;
	}

	public String getCpSelected() {
		return cpSelected;
	}

	public void setCpSelected(String cpSelected) {
		this.cpSelected = cpSelected;
	}

	public List<City> getCities() {
		return cities;
	}

	public void setCities(List<City> cities) {
		this.cities = cities;
	}

	public List<Region> searchRegionLike(final String query) {
		List<Region> regions = cityEJB.findRegionLike(query);
		return regions;
	}

	public List<String> searchCityLikeCP(final String query) {
		List<City> citiesTemp = cityEJB.findCityLikeCP(query);
		List<String> cps = new ArrayList<String>();
		for (City cp : citiesTemp) {
			cps.add(cp.getCodePostal());
		}
		return cps;
	}

	public List<City> searchCityByCP(final String query) {
		cities = cityEJB.findCityByCP(cpSelected);
		return cities;
	}

	public void handleUnselect(final UnselectEvent event) {
		final FacesMessage message = new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Unselected:"
						+ event.getObject().toString(), null);

		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void handleSelectCP(final SelectEvent event) {
		cities = cityEJB.findCityByCP(cpSelected);
	}
}
